<?php
require_once'functions.php';
if(isset($_POST['rate']) && isset($_POST['confID']) && isset($_POST['critID']) && isset($_POST['paperID']))
{
    $conf_id=$_POST['confID'];
    $value=$_POST['rate']; // The value chosen by the reviewer from the radio button
    $id=$_POST['critID']; // the id of the rating criteria chosen from the dropdown list
    //echo $id
    //Selects the rating criteria assigned to the conference by comparing the conference_id of the rate chosen from the dropdownlist
    $query=mysql_query
            ("SELECT rate_id,conference_id FROM conference_rating_criteria WHERE conference_rating_criteria.rate_id ='$id'
             AND conference_rating_criteria.conference_id ='" . $conf_id . "'") or die('here' . mysql_error());
    $row=mysql_fetch_assoc($query);
    $row['rate_id'];
    $span=0;
    $paperID=$_POST['paperID'];
    $verID=get_latest_version($paperID);
    //echo $verID;
    //The query inserts the rate into the database
    $old = mysql_query("SELECT * FROM version_rating_criteria WHERE
        version_id = $verID AND rate_id = $id AND span_id = $span") OR die(mysql_error());
    if(mysql_num_rows($old) == 0) {
    $resultCom=mysql_query("INSERT into version_rating_criteria (version_id,rate_id,span_id,rating_value) VALUES
                ('$verID','$id','$span','$value')") or die(mysql_error());
    } else {
        $resultCom=mysql_query("UPDATE version_rating_criteria SET rating_value = $value
                WHERE version_id = $verID AND rate_id = $id AND span_id = $span") or die(mysql_error());
    } 

    //The average rate will be calculate by dividing sum of the rating value over sum of rate ids
    $sql_query=mysql_query("select COUNT(rate_id) FROM version_rating_criteria WHERE version_id='{$verID}'")
            or die(mysql_error());
    $row=mysql_fetch_assoc($sql_query);
    $x=$row['COUNT(rate_id)']; //getting sum of rate ids
    //getting sum of rate values
    $sql_query2=mysql_query("SELECT SUM(rating_value) FROM version_rating_criteria WHERE version_id='{$verID}'") // sum of all rating values.
            or die(mysql_error());
    $row2=mysql_fetch_assoc($sql_query2);
    $y=$row2['SUM(rating_value)'];
    if(intval($y) == 0)
    {
        $avgRate=0;
    }
    else
    {
        $avgRate=$y / $x; // the average rate(dividing sum of rat value over sum of ids)
    }
    //A query for making sure that the version_id of the paper is the same I have
    $paper_id=mysql_query("SELECT paper_id FROM version WHERE version_id = '" . $verID . "'")
            or die('here' . mysql_error());

    // Inserting the final rate in the tables after making sure of the paper_id and the version of the paper
    $row=mysql_fetch_assoc($paper_id);
    mysql_query("UPDATE paper SET final_rating = '$avgRate' WHERE paper_id = '{$row['paper_id']}'") or die('here1' . mysql_error());
    //echo 'alert("Your rating have been saved successfully");';
}
else
{
    echo "no data";
}
?>
